﻿using System;
using System.Collections.Generic;
using System.Linq;
using TuxedoLib;

namespace SpaceSquirrel.Automation
{
	public class ByatAutomation : SpriteAutomation
	{
		public bool perched = true;
		private int counter = 0;
		private string mode = "perch";
		private int targetY;
		private bool swoopLeft;

		public ByatAutomation(Sprite sprite)
			: base(sprite)
		{ }

		public override void DoStuff(PlayScene scene)
		{
			this.counter++;
			Sprite sprite = this.sprite;
			Tile[][] tiles = scene.Tiles;
			if (this.mode == "perch")
			{
				this.perched = true;
				if (this.counter > 3 * 30)
				{
					this.counter = 0;
					this.mode = "down";
					this.perched = false;
					int dropHeight = (int)(Util.RandomDouble() * 5 + 1) * 16;
					this.targetY = sprite.y + dropHeight;
				}
			}
			else if (this.mode == "down")
			{
				sprite.dy = 2;
				if (sprite.y >= this.targetY || sprite.collideWall || sprite.onGround)
				{
					this.mode = "swoop";
					this.counter = 0;
					this.swoopLeft = scene.Player.x < sprite.x;
				}
			}
			else if (this.mode == "swoop")
			{
				double swoopSpeed = 1.6;
				sprite.dy = Math.Sin(this.counter * 40 / (2 * 3.14159)) * 4;
				sprite.dx = this.swoopLeft ? -swoopSpeed : swoopSpeed;
				if (this.counter > 30 * 6)
				{
					this.mode = "up";
					this.counter = 0;
				}
				else if (sprite.collideWall)
				{
					this.swoopLeft = !this.swoopLeft;
				}
			}
			else if (this.mode == "up")
			{
				sprite.dy = -3.5;
				int mt = sprite.x & 15;
				if (mt < 4)
				{
					sprite.dx = 1;
				}
				else if (mt > 12)
				{
					sprite.dx = -1;
				}
				bool stop = false;
				if (this.sprite.collideWall)
				{
					stop = true;
				}
				else
				{
					int tx = sprite.x / 16;
					int ty = (sprite.y - 8) / 16;
					if (tx < 0 || tx >= scene.cols || ty < 0 || ty >= scene.rows)
					{
						stop = true;
					}
					else
					{
						Tile t = tiles[tx][ty];
						if (t.isTop || t.solid)
						{
							stop = true;
						}
					}
				}

				if (stop)
				{
					this.mode = "perch";
					this.counter = 0;
				}
			}
		}
	}
}
